package io.mft.commons.db.mapper.builder;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Param;

import cn.hutool.core.util.ObjectUtil;
import io.mft.commons.db.mapper.TbEntity;
import io.mft.commons.db.mapper.TbWhere;
import io.mft.commons.db.mapper.annotation.BeanMapper;
import io.mft.commons.db.mapper.annotation.ExpressionParam;
import io.mft.commons.db.mapper.cache.TbDIUMetadataWraper;
import io.mft.commons.db.mapper.dbmethod.DbMapperParameter;
import io.mft.commons.db.mapper.exception.MapperException;
import io.mft.commons.function.response.kv.KVMap;
import io.mft.commons.util.CamelToUnderlineUtil;
import io.mft.commons.util.CommonUtil;

public class TbDIUWraper {
	private TbDIUMetadataWraper tbDIUMetadataWraper;
	private final List<DbMapperParameter> baseMapperParams=new ArrayList<DbMapperParameter>();
	public void setTbDIUMetadataWraper(TbDIUMetadataWraper tbDIUMetadataWraper) {
		this.tbDIUMetadataWraper = tbDIUMetadataWraper;
	}
	public TbDIUMetadataWraper getTbDIUMetadataWraper() {
		return tbDIUMetadataWraper;
	}
	public List<DbMapperParameter> getBaseMapperParams() {
		return baseMapperParams;
	}
	public TbDIUWraper(TbDIUMetadataWraper tbDIUMetadataWraper) {
		this.tbDIUMetadataWraper=tbDIUMetadataWraper;
	}
	public TbEntity getData() {
		TbEntity data=new TbEntity();
		int k=0;
		for (DbMapperParameter dbMapperParameter : baseMapperParams) {
			if (dbMapperParameter.getArgMetadata().getParamAnnotation() instanceof Param) {
				if (null != dbMapperParameter.getValue() && dbMapperParameter.getValue().getClass().isAnnotationPresent(BeanMapper.class)){
					Object object = dbMapperParameter.getValue();
					KVMap t = null;
					if (object instanceof TbEntity) {
						if (k>0) {
							throw new MapperException("TbEntity参数仅支持一个，请检查");
						}
						TbEntity tbEntity=(TbEntity)object;
						t=new KVMap();
						t.putAll(tbEntity);
						if (ObjectUtil.isEmpty(tbDIUMetadataWraper.getTbName())) {
							data.setTableName(tbEntity.getTableName());
						}
						k++;
					}else {
						t=CommonUtil.convertToKVMap(object);
					}
					Set<?> keys=t.keySet();
					for (Object key : keys) {
						if (t.get(key) != null) {
							Object value=t.get(key);
							if (value instanceof Date) {
								Date d=(Date)value;
								value=new Timestamp(d.getTime());
							}
							if (tbDIUMetadataWraper.isNeedCamelToUnderline()) {
								data.set(CamelToUnderlineUtil.camelToUnderline(String.valueOf(key)), value);
							}else {
								data.set(String.valueOf(key), value);
							}
						}
					}
				} else {
					Object value=dbMapperParameter.getValue();
					if (value instanceof Date) {
						Date d=(Date)value;
						value=new Timestamp(d.getTime());
					}
					if (tbDIUMetadataWraper.isNeedCamelToUnderline()) {
						data.set(CamelToUnderlineUtil.camelToUnderline(dbMapperParameter.getArgMetadata().getKey()), value);	
					}else {
						data.set(dbMapperParameter.getArgMetadata().getKey(), value);	
					}
				}
			}
		}
		if (ObjectUtil.isEmpty(data.getTableName())) {
			data.setTableName(tbDIUMetadataWraper.getTbName());
		}
		if (ObjectUtil.isEmpty(data.getTableName())) {
			throw new MapperException("表名不能为空，请检查！");
		}
		return data;
	}
	public List<DbMapperParameter> getExpressionDbMapperParameter() {
		List<DbMapperParameter> list=new ArrayList<DbMapperParameter>();
		for (DbMapperParameter dbMapperParameter : baseMapperParams) {
			if (dbMapperParameter.getArgMetadata().getParamAnnotation() instanceof ExpressionParam) {
				list.add(dbMapperParameter);
			}
		}
		return list;
	}
	public String[] getExpressionColumnArray() {
		int i=0;
		List<DbMapperParameter> list=new ArrayList<DbMapperParameter>();
		for (DbMapperParameter dbMapperParameter : baseMapperParams) {
			if (dbMapperParameter.getArgMetadata().getParamAnnotation() instanceof ExpressionParam) {
				list.add(dbMapperParameter);
			}
		}
		String[] strings=new String[list.size()];
		for (DbMapperParameter dbMapperParameter : list) {
			strings[i]=tbDIUMetadataWraper.isNeedCamelToUnderline()?CamelToUnderlineUtil.camelToUnderline(dbMapperParameter.getArgMetadata().getKey()):dbMapperParameter.getArgMetadata().getKey();
			i++;
		}
		return strings;
	}
	public Object[] getExpressionValueArray() {
		List<DbMapperParameter> list=new ArrayList<DbMapperParameter>();
		int k=0;
		for (DbMapperParameter dbMapperParameter : baseMapperParams) {
			if (dbMapperParameter.getArgMetadata().getParamAnnotation() instanceof ExpressionParam) {
				if (dbMapperParameter.getValue() instanceof TbWhere) {
					if (k>0) {
						throw new MapperException("仅支持一个TbWhere约束对象");
					}
					k++;
				}else {
					list.add(dbMapperParameter);
				}
			}
		}
		int i=0;
		Object[] objects=new Object[list.size()];
		for (DbMapperParameter dbMapperParameter : list) {
			objects[i]=dbMapperParameter.getValue();
			i++;
		}
		return objects;
	}
}
